package com.hpjpw.idoapp.controller;

import com.hpjpw.ido.domain.CheerComment;
import com.hpjpw.ido.domain.CheerMoment;
import com.hpjpw.ido.domain.MessageRemind;
import com.hpjpw.ido.service.ICheerCommentService;
import com.hpjpw.ido.service.ICheerMomentService;
import com.hpjpw.ido.service.IMessageRemindService;
import com.hpjpw.idoapp.service.IDoCheerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.util.resources.cldr.ms.CurrencyNames_ms;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ido/app/cheerComment")
public class IDoCheerCommentController extends BaseController {
    @Autowired
    ICheerCommentService cheerCommentService;
    @Autowired
    IDoCheerService cheerService;
    @Autowired
    ICheerMomentService cheerMomentService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    private IMessageRemindService messageRemindService;

    /**
     * 查询加油站动态评论列表
     */
    @ApiOperation("查询加油站动态评论列表，传入momentId(动态id)，可带分页参数：pageNum=1&pageSize=10")
    @GetMapping("/list/{momentId}")
    public TableDataInfo list(@PathVariable("momentId") Long momentId){
        startPage();
        List<Map<String, String>> mapList = cheerService.commentListResponse(momentId);
        return getDataTable(mapList);
    }

    /**
     * 新增加油站动态的评论
     */
    @ApiOperation("新增加油站动态的评论，接收momentId(动态id),content(评论内容)")
    @PostMapping()
    public AjaxResult addComment(@RequestBody CheerComment cheerComment) {
        /* 安全校验1，无该动态 */
        if (StringUtils.isNull(cheerMomentService.selectCheerMomentById(cheerComment.getMomentId()))){
            return AjaxResult.error("错误！没有此动态，请检查momentId！");
        }
        /* 安全校验2，无此评论者 */
        /*if(StringUtils.isNull(sysUserService.selectUserById(cheerComment.getFromId()))){
            return AjaxResult.error("错误！没有此评论者，请检查fromId！");
        }*/
        /* 安全校验3，评论内容为空 */
        if (StringUtils.isNull(cheerComment.getContent())){
            return AjaxResult.error("错误！评论内容为空，请检查content！");
        }
        Long userId = SecurityUtils.getUserId();
        CheerComment cheerCommentAdd = new CheerComment();
        cheerCommentAdd.setMomentId(cheerComment.getMomentId());
        cheerCommentAdd.setContent(cheerComment.getContent());
        cheerCommentAdd.setFromId(userId);
        cheerCommentAdd.setTime(new Date());
        cheerCommentAdd.setToId(cheerMomentService.selectCheerMomentById(cheerComment.getMomentId()).getReportId());
        cheerCommentService.insertCheerComment(cheerCommentAdd);

        MessageRemind messageRemind = new MessageRemind(cheerCommentAdd, SecurityUtils.getUsername());
        messageRemindService.insertMessageRemind(messageRemind);

        return AjaxResult.success();
    }
    /**
     * 删除加油站评论
     */
    @ApiOperation("删除加油站评论，接收momentId(动态id),id(评论id)")
    @DeleteMapping()
    public AjaxResult deleteComment(@RequestBody CheerComment cheerComment) {
        /* 安全校验1，无此动态 */
        CheerMoment cheerMomentCheck = cheerMomentService.selectCheerMomentById(cheerComment.getMomentId());
        if (StringUtils.isNull(cheerMomentCheck)){
            return AjaxResult.error("错误！无此动态，请检查momentId");
        }
        /* 安全校验2，无此评论 */
        CheerComment cheerCommentCheck = cheerCommentService.selectCheerCommentById(cheerComment.getId());
        if (StringUtils.isNull(cheerCommentCheck)){
            return AjaxResult.error("错误！无此评论，请检查id");
        }
        /* 安全校验3，删除操作者非评论发布者 */
        Long userId = SecurityUtils.getUserId();
        if (!(cheerCommentCheck.getFromId().equals(userId))){
            return AjaxResult.error("错误！没有权限无法删除");
        }
        cheerCommentService.deleteCheerCommentById(cheerCommentCheck.getId());
        return AjaxResult.success();
    }
}
